#include "CQuadTree.h"
#include "my_utilities/mischelper.h"

using namespace std;

CQuadTree::CQuadTree(CRect r, int maxI) :
    worldRect_(r),
    maxItems_(maxI),
    headNode_(CQuadTreeNode(0, r, maxI, 0)) //initialize important variable members
{

}

//inserts item into quad tree

void CQuadTree::Insert(CQuadTreePosItem* item)
{
    if(!headNode_.ContainsRect(item->GetRect()))
    {
        assert(false);
    }

    headNode_.Insert(item);
}

////insets item into quad tree
//
//void CQuadTree::Insert(CRigidBody* par, Vec2f* ps)
//{
//    CQuadTreePosItem item(par, ps);
//
//    if (!headNode_.ContainsRect(item.GetRect()))
//    {
//        assert(false);
//    }
//
//
//    headNode_.Insert(item);
//}

/* Query Methods */
//gets list of items containing this point

void CQuadTree::QueryItems(const Vec2f& point, vector<CQuadTreePosItem*>& itemsFound)
{
    headNode_.QueryItems(point, itemsFound);
}

//gets list of items intersecting rect

void CQuadTree::QueryItems(const CRect& r,  vector<CQuadTreePosItem*>& itemsFound)
{
    headNode_.QueryItems(r, itemsFound);
}

//DEBUG/Dave

void CQuadTree::DebugQueryItems(const Vec2f& point, vector<CQuadTreePosItem* >& itemsFound, Vec2f& identifier, unsigned int& debugDepth)
{
    headNode_.DebugQueryItems(point, itemsFound, identifier, debugDepth);
}

void CQuadTree::DebugQueryItemsInParent(const Vec2f& point, vector <CQuadTreePosItem* >& itemsFound, Vec2f& identifier, unsigned int& debugDepth)
{
    headNode_.DebugQueryItemsInParent(point, itemsFound, identifier, debugDepth);
}

void CQuadTree::DebugPrintItems()
{
    headNode_.DebugPrintItems();
}

//gets list of all items in this node

void CQuadTree::GetAllItems(vector<CQuadTreePosItem*>& itemsFound)
{
    headNode_.GetAllItems(itemsFound);
}